package com.glory.brepda.activity.mfg;

import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.fragment.app.FragmentManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.brepda.R;
import com.glory.brepda.adapter.EqpIDLEAdapter;
import com.glory.brepda.adapter.MultiEqpAdapter;
import com.glory.brepda.base.BaseActivity;
import com.glory.brepda.callback.CustomDialogEntityCallBack;
import com.glory.brepda.entity.MultiEqpBean;
import com.glory.brepda.entity.ToolBomLineEntity;
import com.glory.brepda.entity.UpdateIPEntity;
import com.glory.brepda.entity.response.BaseResponseBean;
import com.glory.brepda.utils.NetUtils;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_CommonCallback;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralPop;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DialogUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_PopUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class EqpIDLETypeActivity extends BaseActivity {

    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.etEqpid)
    FreeUI_ClearEditText etEqpId;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.ivIdleType)
    ImageView ivIdleType;
    @BindView(R.id.flIDLEType)
    FrameLayout flIDLEType;
    @BindView(R.id.rvEqpList)
    RecyclerView rvEqpList;
    @BindView(R.id.tvIDLETypeText)
    TextView tvIDLETypeText;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    private FreeUI_GeneralPop codePop;
    private List<String> lockList;
    private String idleType = "待料";
    private EqpIDLEAdapter mMultiEqpAdapter;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_eqp_status_idletype;
    }

    @Override
    protected void initView() {
        setMainLayout(llMain);
        String title = getIntent().getStringExtra(FreeApi_StaticMembers.SHOW_PLUGIN_KEY);
        tvTitle.setText(title);
        tvBaseSubmit.setText("设备待机");
        lockList = new ArrayList<>();
        lockList.add("待料");
        lockList.add("关机");
        initRecyclerView(rvEqpList, this);
        mMultiEqpAdapter = new EqpIDLEAdapter(null,this );
        mMultiEqpAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        mMultiEqpAdapter.isFirstOnly(true);
        mMultiEqpAdapter.setOnItemClickListener((baseQuickAdapter, view, i) -> {
            mMultiEqpAdapter.remove(i);
        });
        rvEqpList.setAdapter(mMultiEqpAdapter);

        initEdit(etEqpId, s->{
            for (MultiEqpBean sMultiEqpBean : mMultiEqpAdapter.getData()){
                if (sMultiEqpBean.getEQUIPMENTID().equals(s)){
                    showToast("当前设备已添加到列表",false);
                    return;
                }
            }
            MultiEqpBean multiEqpBean = new MultiEqpBean();
            multiEqpBean.setEQUIPMENTID(getText(etEqpId));
            mMultiEqpAdapter.addData(0,multiEqpBean);

        });
    }

    @Override
    protected void initData() {

    }

    @OnClick({R.id.tvBaseSubmit,R.id.flIDLEType})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tvBaseSubmit:
                if (mMultiEqpAdapter.getData().size()==0){
                showToast("请先扫描设备ID",false);
                return;
            }
                FreeApi_DialogUtils.getConfirmAndCancelDialog(this, "确定要进入"+idleType+"待机状态吗?", new FreeUI_CommonCallback() {
                    @Override
                    public void onSuccess() {
                        FragmentManager supportFragmentManager = getSupportFragmentManager();
                        FreeUI_EntityCallBack<BaseResponseBean<JSONObject>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<JSONObject>>
                                (new TypeToken<BaseResponseBean<JSONObject>>() {
                                }.getType(), getSupportFragmentManager(), this) {

                            @Override
                            public void onSuccess(Response<BaseResponseBean<JSONObject>> response) {
                                super.onSuccess(response);
                                if (response.body().isSuccess(getApplicationContext(),false,getSupportFragmentManager())){
                                    showToast("待机成功！", false);
                                    finish();
                                }
                            }

                            @Override
                            public void onError(Response<BaseResponseBean<JSONObject>> response) {
                                super.onError(response);
                                loadError(response.getException(),"IDLE");
                            }
                        };
                        String messageName = "IF.BREEQPSTATUSQUERY";
                        String method = "BRESTANDBYIDLERECORD";
                        JsonObject map = new JsonObject();
                        map.addProperty("EQUIPMENTID",getText(etEqpId));
                        map.addProperty("IDLETYPE",idleType);
                        map.addProperty("ACTION","IN");
                        map.add("EQUIPMENTLIST", new JsonParser().parse(new Gson().toJson(mMultiEqpAdapter.getData())).getAsJsonArray());
                        NetUtils.requestPostNet(messageName,method, EqpIDLETypeActivity.this,map,callBack);
                    }
                }, () -> {

                }).show(getSupportFragmentManager(),"");
                break;
            case R.id.flIDLEType:

                if (codePop != null) {
                    if (codePop.isShowing()) {
                        codePop.dismiss();
                    }
                } else {
                    codePop = FreeApi_PopUtils.getNormalPop(getApplicationContext(), flIDLEType.getWidth(), lockList, ivIdleType, (codeS, i) -> {
                        tvIDLETypeText.setText(codeS);
                        idleType = codeS;
                    });
                }
                FreeApi_PopUtils.setArrowShow(ivIdleType);
                codePop.showAsDropDown(flIDLEType, 0, 0);
                break;

        }

    }
}
